﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.DPE.UK.TFS.EmbeddedWorkItems.Common;

namespace Microsoft.DPE.UK.TFS.EmbeddedWorkItems.UnitTests
{
    /// <summary>
    /// Unit tests to check the Logic class, which parses the checkin comment for work items.
    /// 
    /// Currently provides 100% code coverage.
    /// </summary>
    [TestClass]
    public class LogicTests
    {
        public LogicTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void SimpleCommentOneWINoSuffix()
        {
            string comment = "Fixed bug [123]";
            int[] expected = { 123 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentOneWIAndSuffix()
        {
            string comment = "Fixed bug [123] now";
            int[] expected = { 123 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentOneWIAndRandomNumber()
        {
            string comment = "Fixed 1 bug [123] now";
            int[] expected = { 123 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentTwoWIsAndSuffix()
        {
            string comment = "Fixed bugs [123][456] now";
            int[] expected = { 123, 456 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentTwoWIsAndWhiteSpace()
        {
            string comment = "Fixed bugs [ 123    ][  456    ] now";
            int[] expected = { 123, 456 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentTenWIsAndRandomNumber()
        {
            string comment = "Fixed 10 bugs [123][234][345][456][567][678][789][890][901][102] now";
            int[] expected = { 123, 234, 345, 456, 567, 678, 789, 890, 901, 102 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentNoWIs()
        {
            string comment = "Fixed some bugs";
            int[] expected = { };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SimpleCommentTenWIsWhiteSpacesAndRandomNumber()
        {
            string comment = "Fixed 10 bugs [ 123][234  ][345   ][  456][  567][678   ][789  ][ 890][901][    102                       ] now";
            int[] expected = { 123, 234, 345, 456, 567, 678, 789, 890, 901, 102 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void MultipartCommentTwoWIs()
        {
            string comment = "Fixed some bugs including [123] and [456] in this check-in.";
            int[] expected = { 123, 456 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void WIsFirst()
        {
            string comment = "[123] [456] fixed.";
            int[] expected = { 123, 456 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void InvalidWIs()
        {
            string comment = "Fixed some bugs including [123] and [abc] in this check-in.";
            int[] expected = { 123 };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void TooLargeWI()
        {
            string comment = "Fixed bug [12300000000000000000000000000000000000000000000000000] in this check-in.";
            int[] expected = { };

            int[] actual = Logic.getWorkItemIds(comment);
            CollectionAssert.AreEqual(expected, actual);
        }
    }
}
